(use-package browse-url
  :hook
  (after-init . qrthi/select-text-browser)
  (after-init . qrthi/defalias-webjump-url-fix)
  :bind
  (:prefix-map qrthi/browse-url-prefix
	       :prefix "C-c u b"
	       ("b" . browse-url)
	       ("i" . qrthi/browse-url-image-viewer)
	       ("g" . qrthi/browse-url-graphical-browser)
	       ("j" . webjump)
	       )
  :init
  (defun qrthi/select-text-browser ()
    (if (executable-find "w3m")
	(defalias 'qrthi/browse-url-text-browser 'qrthi/browse-url-w3m)
      (defalias 'qrthi/browse-url-text-browser 'eww-browse-url))
    (defalias 'qrthi/browse-url-graphical-browser 'qrthi/browse-url-luakit)
    (defalias 'qrthi/browse-url-image-viewer'qrthi/browse-url-mpv)

    )

  (defun qrthi/defalias-webjump-url-fix ()
    ;; no idea why this doesn't work
    (defalias 'webjump-url-fix 'qrthi/webjump-url-fix))

  (defun qrthi/webjump-url-fix (url)
    (if (webjump-null-or-blank-string-p url) ""
      (webjump-url-fix-trailing-slash
       (cond
	((string-match "^[a-zA-Z]+:" url) url)
	((string-match "^/" url) (concat "file://" url))
	((string-match "^\\([^\\./]+\\)" url)
	 (concat (if (string= (downcase (match-string 1 url)) "ftp")
		     "ftp" "https") "://" url))	(t url)))))

  (defun qrthi/browse-url-mpv (url &optional new-window)
    "Stream files with mpv"
    (interactive (browse-url-interactive-arg "URL: "))
    (message "Opening %s with mpv..." url)
    (start-process "mvi" nil "mpv"
		   "--no-config" "--loop-file=inf" "--force-window=immediate" url))

  (defun qrthi/browse-url-luakit (url &optional new-window)
    (interactive (browse-url-interactive-arg "URL: "))
    (message "Opening %s with luakit..." url)
    (start-process "luakit" nil "luakit"
		   url))

  (defun qrthi/browse-url-qutebrowser (url &optional new-window)
    (interactive (browse-url-interactive-arg "URL: "))
    (message "Opening %s with qutebrowser..." url)
    (start-process "qutebrowser" nil "qutebrowser"
		   "--temp-basedir"
		   ":set content.autoplay false"
		   ":set content.canvas_reading false"
		   ;; ":set content.cookies.accept no-3rdparty" ; canvas needs cookies
		   ;; ":set content.cookies.store false"
		   ":set content.default_encoding utf8"
		   ":set content.geolocation false"
		   ":set content.host_blocking.enabled false"
		   ":set content.persistent_storage false"
		   ":set content.webgl false"
		   ":set downloads.location.prompt false"
		   ":set downloads.position bottom"
		   ":set editor.command \"['emacsclient', '{file}']\""
		   ":set hints.chars \"aoeuidhtns\""
		   ":set new_instance_open_target window"
		   ":set scrolling.bar never"
		   ":set tabs.position bottom"
		   ":set tabs.show never"
		   ":set tabs.tabs_are_windows true"
		   ;; ":set content.cache.appcache false"
		   ;; ":set content.frame_flattening true"
		   ;; ":set content.images false"
		   ;; ":set content.javascript.enabled false"
		   ;; ":set content.notifications false"
		   ;; ":set downloads.open_dispatcher \"wget\""
		   ;; ":set hints.find_implementation javascript"
		   ;; ":set hints.mode word"
		   ;; ":set statusbar.hide true"
		   ;; ":set window.hide_decoration true"
		   (concat ":set downloads.location.directory "  qrthi/directory-downloads)
		   ;; ":bind f nop"
		   ":bind g reload"
		   ;; ":bind <Alt-g>o hint all normal"
		   ;; ":bind <Alt-g>O hint all tab"
		   ;; ":bind <Alt-g>r hint all right-click"
		   ;; ":bind <Alt-g>h hint all hover"
		   ;; ":bind <Alt-g>y hint links yank"
		   ;; ":bind <Alt-g>d hint links download"
		   ;; ":bind <Alt-g>m hint links spawn mpv {hint-url}"
		   ;; ":bind <Ctrl-j> yank"
		   ;; ":bind <Ctrl-Alt-j> edit-url"
		   ":bind <Ctrl-f> search-next"
		   ;; ":bind <Ctrl-r> search-prev"
		   ;; ":bind <Ctrl-/> undo"
		   ":bind l back"
		   ":bind r forward"
		   ":bind H history"
		   ":bind v view-source"
		   ;; ":bind && open-editor"
		   ;; window.title_format (Current: {perc}{current_title}{title_sep}qutebrowser)
		   url))

  (defun qrthi/browse-url-surf (url &optional new-window)
    "acgDIsVMf"
    (interactive (browse-url-interactive-arg "URL: "))
    (message "Opening %s with acgDISVMf..." url)
    (start-process "surf" nil "surf"
		   "-aa"	; deny cookies
		   "-S"	; enable js
		   ;; "-i"	; disable images
		   ;; "-u" qrthi/user-agent
		   "-b"	; disable scrollbar
		   url))

  :custom
  (browse-url-browser-function
   '((".+\\.jp\\(e\\)?g$" . qrthi/browse-url-image-viewer)
     (".+\\.png$" . qrthi/browse-url-image-viewer)
     (".+\\.webm$" . qrthi/browse-url-image-viewer)
     (".+\\.gif$" . qrthi/browse-url-image-viewer)
     ("ankiweb.net" . qrthi/browse-url-graphical-browser)
     ("\\(www\\.\\)?youtube\\.com" . qrthi/browse-url-graphical-browser)
     ("ankiweb\\.net" . qrthi/browse-url-graphical-browser)
     ("duckduckgo\\.com\\/\\?.+iax=images.*&q=.+" . qrthi/browse-url-graphical-browser)
     ("what-ch\\.mooo\\.com" . qrthi/browse-url-graphical-browser)
     ("cock\\.li" . qrthi/browse-url-graphical-browser)
     ("gnfos\\.com" . qrthi/browse-url-graphical-browser)
     ("gog\\.com" . qrthi/browse-url-graphical-browser)
     ("landingscu\\.org" . qrthi/browse-url-graphical-browser)
     ("thepiratebay\\.org" . qrthi/browse-url-graphical-browser)
     ("nixos.org.+options\\.html" . qrthi/browse-url-graphical-browser)
     ("learn\\.maricopa\\.edu" . qrthi/browse-url-graphical-browser)
     ("lutris\\.net" . qrthi/browse-url-graphical-browser)
     ("tutanota\\.com" . qrthi/browse-url-graphical-browser)
     ("notabug\\.org" . qrthi/browse-url-graphical-browser)
     ("winehq\\.org" . qrthi/browse-url-graphical-browser)
     ("ebay\\.com" . qrthi/browse-url-graphical-browser)
     ("quizlet\\.com" . qrthi/browse-url-graphical-browser)
     ("lolcow\\.farm" . qrthi/browse-url-graphical-browser)
     ("craigslist\\.org" . qrthi/browse-url-graphical-browser)
     ("kiwifarms\\.net" . qrthi/browse-url-graphical-browser)
     ("melpa\\.org" . qrthi/browse-url-graphical-browser)
     ("192.168.1.1" . qrthi/browse-url-graphical-browser)
     ("." . qrthi/browse-url-text-browser)))

  (webjump-sites '( ;; Query sites
		   ("DuckDuckGo Lite" .
		    [simple-query "duckduckgo.com/lite" ;duckduckgo.com
				  "duckduckgo.com/lite?&kp=-2&kd=-1&kv=1&k1=-1&q=" ;kh=1& &kz=-1
				  ""])
		   ("DuckDuckGo Images" .
		    [simple-query "duckduckgo.com/" ;duckduckgo
				  "duckduckgo.com/?&iax=images&ia=images&kp=-1&kh=1&k1=-1&q="
				  ""])
		   ("Anki Web" .
		    [simple-query "ankiweb.net/"
				  "ankiweb.net/shared/decks/"
				  ""])
		   ("Bandcamp" .
		    [simple-query "bandcamp.com"
				  "bandcamp.com/search?q="
				  ""])
		   ("BitChute" .
		    [simple-query "bitchute.com"
				  "search.bitchute.com/&query="
				  ""])
		   ("Cataclysm: Dark Days Ahead Wiki" .
		    [simple-query "cddawiki.chezzo.com/cdda_wiki/index.php?title=Main_Page"
				  "cddawiki.chezzo.com/cdda_wiki/index.php?search="
				  "&title=Special%3ASearch&go=Go"
				  ])
		   ("Christgau Search" .
		    [simple-query "robertchristgau.com/"
				  "robertchristgau.com/get_artist.php?name="
				  ""])
		   ("Circuit Specialists" .
		    [simple-query "circuitspecialists.com"
				  "circuitspecialists.com/search.html?searchQuery="
				  ""])
		   ("CiteSeer Citation Search" .
		    [simple-query "citeseer.nj.nec.com"
				  "citeseer.nj.nec.com/cs?q="
				  "&submit=Search+Citations&cs=1"])
		   ("CiteSeer Document Search" .
		    [simple-query "citeseer.nj.nec.com"
				  "citeseer.nj.nec.com/cs?q="
				  "&submit=Search+Documents&cs=1"])
		   ("Danbooru" .
		    [simple-query "danbooru.me/explore/posts/popular"
				  "danbooru.me/posts?tags="
				  ""])
		   ("e621" .
		    [simple-query "e621.net"
				  "e621.net/posts?tags="
				  ""])
		   ("Feedbooks" .
		    [simple-query "feedbooks.com/publicdomain"
				  "feedbooks.com/search?protection=drmfree&query="
				  ""])
		   ("Gelbooru" .
		    [simple-query "gelbooru.com/"
				  "gelbooru.com/index.php?page=post&s=list&tags="
				  ""])
		   ("Head-fi" .
		    [simple-query "head-fi.org/"
				  "head-fi.org/search/?q="
				  "&o=relevance"])
		   ("Library Genesis" .
		    [simple-query "libgen.is/"
				  "libgen.is/search.php?req="
				  ""])
		   ("Lutris" .
		    [simple-query "lutris.net"
				  "lutris.net/games?q="
				  ""])
		   ("Interactive Fiction Database" .
		    [simple-query "ifdb.tads.org"
				  "ifdb.tads.org/search?searchbar="])
		   ("NewEgg" .
		    [simple-query "newegg.com"
				  "newegg.com/p/pl?d="
				  ""])
		   ("Nitter" .
		    [simple-query "nitter.net"
				  "nitter.net/"
				  ""])
		   ("Paheal.net" .
		    [simple-query "rule34.paheal.net/"
				  "rule34.paheal.net/post/list/"
				  "/1"])
		   ("Pirate Bay" .
		    [simple-query "thepiratebay.org"
				  "thepiratebay.org/search.php?q="
				  "&all=on&search="])
		   ("Project Gutenberg" .
		    [simple-query "gutenberg.org"
				  "gutenberg.org/ebooks/search/?query="
				  ""])
		   ("Quizlet" .
		    [simple-query "quizlet.com/"
				  "quizlet.com/subject/"
				  ""])
		   ("Rebecca Black Tech" .
		    [simple-query "rbt.asia/"
				  "rbt.asia/_/search/text/"
				  ""])
		   ("Standard Ebooks" .
		    [simple-query "standardebooks.org"
				  "standardebooks.org/ebooks?query="
				  ""])
		   ("SWFchan" .
		    [simple-query "swfchan.com"
				  "eye.swfchan.com/search/?q="
				  ""])
		   ("ThinkWiki" .
		    [simple-query "thinkwiki.org"
				  "thinkwiki.org/w/index.php?search="
				  ""])
		   ("TinEye" .
		    [simple-query "tineye.com"
				  "tineye.com/search/"
				  "?sort=score&order=desc&page=1"])
		   ("Wikibooks" .
		    [simple-query "wikibooks.org"
				  "en.wikibooks.org/w/index.php?search="
				  ""])
		   ("Wikipedia" .
		    [simple-query "wikipedia.org"
				  "https://en.wikipedia.org/wiki/Special:Search?search="
				  ""])
		   ("Wine" .
		    [simple-query "winehq.org"
				  "winehq.org/search?q="
				  ""])
		   ("YouTube" .
		    [simple-query "youtube.com"
				  "youtube.com/results?search_query="
				  ""])
		   ;; Forums
		   ("/prog/" . "dis.tinychan.net/prog/")
		   ("/what/ - What..." . "what-ch.mooo.com/what")
		   ("Bunbunmaru" . "bunbunmaru.com/wakaba/")
		   ("GNFOS" . "gnfos.com/jp")
		   ("Kiwi Farms" . "kiwifarms.net")
		   ("Lolcow" . "lolcow.farm")
		   ("Melpa" . "melpa.org")
		   ("Nixers" . "nixers.net")
		   ("Ota" . "ota-ch.com")
		   ("Secret Area of VIP Quality" . "secretareaofvipquality.org/saovq/")
		   ;; Bookmarks
		   ("Cockmail" . "mail.cock.li")
		   ("Pine64" . "pine64.org")
		   ("Bookshop" . "bookshop.org")
		   ("Music Theory" . "musictheory.net/lessons")
		   ("Canvas" . "learn.maricopa.edu")
		   ("Crowd Supply" . "crowdsupply.com")
		   ("GOG" . "gog.com")
		   ("Gitea" . "gitea.org")
		   ("I Love Languages" . "ilovelanguages.org")
		   ("Landings" . "landingscu.org")
		   ("LuCi" . "192.168.1.1")
		   ("Mesa Community College" . "mesacc.edu")
		   ("Mobile NixOS" . "mobile.nixos.org")
		   ("Nix Pills" . "nixos.org/guides/nix-pills/index.html")
		   ("NixOS Options" . "nixos.org/nixos/options.html")
		   ("NixOS Learn" . "nixos.org/learn.html")
		   ("Not a Bug" . "notabug.org")
		   ("postmarketOS" . "postmarketos.org")
		   ("Ruwix Wiki" . "ruwix.com")
		   ("Suckless" . "suckless.org")
		   ("Ting" . "ting.com")
		   ("Tutanota" . "tutanota.com")
		   )))
